/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.ObjectNode;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.diagram.activity.helper.NodePinHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;
import com.cea.papyrus.umlutils.NamedElement;

/**
 * Controller of an InputPin
 *
 * @author Gilles Cannenterre
 * @since 27 mars 07
 */
public class InputPinEditPart extends ObjectNodeEditPart {

	public InputPinEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode, modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public InputPin getUmlElement() {
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		// Case of element import
		if(e instanceof ElementImport){
			return (InputPin)((ElementImport) e).getImportedElement();
		}
		// This is a simple InputPin
		return (InputPin)e;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getDragTracker(org.eclipse.gef.Request)
	 */
	@Override
	public DragTracker getDragTracker(Request request) {
		return new ObjectNodeTracker(this);
	}

	/**
	 * this method calculate the good position of a port by taking account of his parent.
	 * 
	 * @param point the position of the current port ins absolute coordinate
	 * 
	 * @return the new position of the port.
	 */
	public Point inRunnerOfClass(Point point){
		Dimension inputPinDimension = getGraphNode().getSize();
		//apply good position , the defaut is the same point
		NodePinHelper np= new NodePinHelper();
		return np.inRunner((GraphNode)getGraphNode().getSemanticParent(), point, inputPinDimension);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);
		
		IFigure fig = getFigure();
	
		// Classifier
		if (fig instanceof IconFigure) {
			IconFigure icf = (IconFigure) getFigure();
			icf.setName(((NamedElement) getUmlElement()).getName());
		} else {
			ObjectNode currentObjectNode = getUmlElement();
			((NodeFigure) fig).refreshColors(getGraphNode());
			((NodeFigure) fig).refreshPresentation(getPresentation(),currentObjectNode, getGraphNode());
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}
	
    /**
     * Return false.
     * This method is used in {@link com.cea.papyrus.core.editpart.LayoutEditPolicy} in order to
     * know how to compute the coordinate of a added part.
     * @TODO move this method to more appropriate place. 
     * @return
     */
    @Override
    public boolean needRelativeTranslationForAddCommand()
    {
        return false;
    }
	
}
